package com.example.demoexternalusagediceroller;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.example.demoexternalusagediceroller.DiceRollerReceiver.CallbackGetGamesResponse;
import com.example.demoexternalusagediceroller.DiceRollerReceiver.CallbackGetPresetsResponse;

public class MainActivity extends Activity {
	private Spinner cbGames;
	private Spinner cbPresets;
	private String[] gamePaths;
	private String[] presetIds;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		cbGames = (Spinner) findViewById(R.id.cbGames);
		cbPresets = (Spinner) findViewById(R.id.cbPresets);

		cbGames.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent();
				intent.setClassName("org.amphiprion.diceroller", "org.amphiprion.diceroller.service.DiceRollerService");
				intent.setAction("org.amphiprion.diceroller.GetPresets");
				intent.putExtra("GAME_PATH", gamePaths[position]);
				intent.putExtra("CALLBACK_ACTION", "org.amphiprion.diceroller.GetPresetsResponse");
				ComponentName cn = startService(intent);

			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {

			}
		});

		DiceRollerReceiver.callbackGetGamesResponse = new CallbackGetGamesResponse() {

			@Override
			public void onGamesResponse(String[] names, String[] paths) {
				gamePaths = paths;
				ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, names);
				cbGames.setAdapter(adapter);
			}
		};

		DiceRollerReceiver.callbackGetPresetsResponse = new CallbackGetPresetsResponse() {

			@Override
			public void onPresetsResponse(String[] ids, String[] names) {
				presetIds = ids;
				String[] pNames = new String[names.length + 1];
				pNames[0] = "- None -";
				for (int i = 0; i < names.length; i++) {
					pNames[i + 1] = names[i];
				}
				ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, pNames);
				cbPresets.setAdapter(adapter);

			}
		};

		Intent intent = new Intent();
		intent.setClassName("org.amphiprion.diceroller", "org.amphiprion.diceroller.service.DiceRollerService");
		intent.setAction("org.amphiprion.diceroller.GetGames");
		intent.putExtra("CALLBACK_ACTION", "org.amphiprion.diceroller.GetGamesResponse");
		ComponentName cn = startService(intent);
		// Log.d("TEST SERVICE", "cn=" + cn.getClassName());

		// // call premier jeu
		// intent = new Intent();
		// intent.setClassName("org.amphiprion.diceroller", "org.amphiprion.diceroller.service.DiceRollerService");
		// intent.setAction("org.amphiprion.diceroller.GetPresets");
		// intent.putExtra("GAME_PATH", "/storage/sdcard0/Amphiprion/diceRoller/games/DescentV2");
		// intent.putExtra("CALLBACK_ACTION", "org.amphiprion.diceroller.GetPresetsResponse");
		// cn = startService(intent);
		// Log.d("TEST SERVICE", "cn=" + cn.getClassName());

		Button btRoll = (Button) findViewById(R.id.btRoll);
		btRoll.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				int gameIndex = cbGames.getSelectedItemPosition();
				int presetIndex = cbPresets.getSelectedItemPosition() - 1;
				if (gameIndex >= 0) {
					Intent intent = new Intent();
					intent.setClassName("org.amphiprion.diceroller", "org.amphiprion.diceroller.LauncherActivity");
					intent.putExtra("GAME_DIR", gamePaths[gameIndex]);
					if (presetIndex >= 0) {
						intent.putExtra("GAME_PRESET", presetIds[presetIndex]);
					}
					startActivity(intent);
				}
			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
